﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DDD;
using Xunit;

namespace DokiDokiTest {
    public class TestIndexBuffer {
        [Fact]
        public void Test_New_Point () {
            var ibuf = new IndexBuffer<int> (PrimitiveType.Point, 2);

            Assert.Equal (PrimitiveType.Point, ibuf.PrimitiveType);
            Assert.Equal (2, ibuf.PrimitiveCount);
            Assert.Equal (1, ibuf.Cpp);
            Assert.Equal (IndexType.Int, ibuf.IndexType);
            Assert.Equal (2, ibuf.IndexCount);
        }

        [Fact]
        public void Test_New_Line () {
            var ibuf = new IndexBuffer<int> (PrimitiveType.Line, 2);

            Assert.Equal (PrimitiveType.Line, ibuf.PrimitiveType);
            Assert.Equal (2, ibuf.PrimitiveCount);
            Assert.Equal (2, ibuf.Cpp);
            Assert.Equal (IndexType.Int, ibuf.IndexType);
            Assert.Equal (4, ibuf.IndexCount);
        }

        [Fact]
        public void Test_New_Triangle () {
            var ibuf = new IndexBuffer<int> (PrimitiveType.Triangle, 2);

            Assert.Equal (PrimitiveType.Triangle, ibuf.PrimitiveType);
            Assert.Equal (2, ibuf.PrimitiveCount);
            Assert.Equal (3, ibuf.Cpp);
            Assert.Equal (IndexType.Int, ibuf.IndexType);
            Assert.Equal (6, ibuf.IndexCount);
        }

        [Fact]
        public void Test_New_Type () {
            var ibuf_byte  = new IndexBuffer<byte> (PrimitiveType.Triangle, 2);
            var ibuf_sbyte = new IndexBuffer<sbyte> (PrimitiveType.Triangle, 2);
            var ibuf_short = new IndexBuffer<short> (PrimitiveType.Triangle, 2);
            var ibuf_ushort = new IndexBuffer<ushort> (PrimitiveType.Triangle, 2);
            var ibuf_int = new IndexBuffer<int> (PrimitiveType.Triangle, 2);
            var ibuf_uint = new IndexBuffer<uint> (PrimitiveType.Triangle, 2);

            Assert.Equal (IndexType.Byte, ibuf_byte.IndexType);
            Assert.Equal (IndexType.SByte, ibuf_sbyte.IndexType);
            Assert.Equal (IndexType.Short, ibuf_short.IndexType);
            Assert.Equal (IndexType.UShort, ibuf_ushort.IndexType);
            Assert.Equal (IndexType.Int, ibuf_int.IndexType);
            Assert.Equal (IndexType.UInt, ibuf_uint.IndexType);
        }

        /// <summary>
        /// Get/Setの基本的なテスト
        /// </summary>
        [Fact]
        public void Test_GetSet_1 () {
            var ibuf = new IndexBuffer<int> (PrimitiveType.Triangle, 2);
            var indices = new int[] { 1, 2, 3, 4, 5, 6 };
            var buf = new int[6];

            ibuf.Set (0, 2, indices);
            ibuf.Get (0, 2, buf);

            Assert.Equal (indices, buf);
        }

        /// <summary>
        ///  中途半端な位置のGet/Setのテスト
        /// </summary>
        [Fact]
        public void Test_GetSet_2 () {
            var ibuf = new IndexBuffer<int> (PrimitiveType.Triangle, 10);
            var indices = new int[] { 1, 2, 3, 4, 5, 6 };
            var buf = new int[6];

            ibuf.Set (5, 2, indices);
            ibuf.Get (5, 2, buf);

            Assert.Equal (indices, buf);
        }

        /// <summary>
        /// 列挙子のテスト
        /// </summary>
        [Fact]
        public void Test_Indices () {
            var ibuf = new IndexBuffer<int> (PrimitiveType.Triangle, 2);
            var indices = new int[] { 1, 2, 3, 4, 5, 6 };
            ibuf.Set (0, 2, indices);

            Assert.Equal (21, ibuf.Indices.Sum ());
        }

        /// <summary>
        /// 暗黙的な列挙子のテスト
        /// </summary>
        [Fact]
        public void Test_Enumerator () {
            var ibuf = new IndexBuffer<int> (PrimitiveType.Triangle, 2);
            var indices = new int[] { 1, 2, 3, 4, 5, 6 };
            ibuf.Set (0, 2, indices);

            Assert.Equal (21, ibuf.Sum ());
        }
    }
}
